package com.j1903.controller;

import com.j1903.util.JsonResult;
import com.j1903.service.GoodsService;
import com.j1903.bean.Goods;
import java.util.List;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cy
 * @since 2021-12-13
 */
@RestController
@RequestMapping("/goods")
    public class GoodsController {
@Resource
private GoodsService goodsService;
@RequestMapping("/add")
public JsonResult add(@RequestBody Goods obj){
    goodsService.save(obj);
        return new JsonResult(200,"添加成功",
        obj.getId());
        }
@RequestMapping("/upd")
public JsonResult upd(@RequestBody Goods obj){
        Boolean rs=goodsService.saveOrUpdate(obj);
        if(rs){
        return new JsonResult(200,"更新成功");
        }
        else{
        return new JsonResult(500,"更新失败");
        }
        }
@RequestMapping("/delById")
public JsonResult delById(Long id){
        Boolean rs=goodsService.removeById(id);
        if(rs){
        return new JsonResult(200,"删除成功");
        }
        else{
        return new JsonResult(500,"删除失败");
        }
        }
@RequestMapping("/list")
public JsonResult list(@RequestBody Goods obj){
        List<Goods> list=goodsService.lists(obj);
        return new JsonResult(200,"查询成功",list);
        }
@RequestMapping("/page/{curPage}/{pageRecords}")
public JsonResult page(@RequestBody Goods obj,
@PathVariable int curPage,
@PathVariable int pageRecords){
        IPage<Goods> ipage=new Page<>(curPage,pageRecords);
        IPage<Goods> page=goodsService.pages(ipage,obj);
        return new JsonResult(200,"查询分页成功",page);
        }
        }

